package com.alameda.common.config.mybatis;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mybatis plus 配置类
 *
 * @author Lv
 * @date 2024/2/19
 */
@Configuration
@MapperScan("com.alameda.mapper")
public class MybatisPlusConfig {

    /**
     * MybatisPlus拦截器
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 加载数据隔离拦截器
        interceptor.addInnerInterceptor(new DataPermissionInterceptor(new CustomDataPermissionInterceptor()));
        // 加载分页拦截器 指定数据库类型为 MySql
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * 设定自动填充拦截器
     *
     * @param sqlSessionFactory sql工厂类
     * @return
     */
    @Bean
    public Boolean setAutoFillValueInterceptor(SqlSessionFactory sqlSessionFactory) {
        org.apache.ibatis.session.Configuration configuration = sqlSessionFactory.getConfiguration();
        configuration.addInterceptor(new MybatisAutoFillValueInterceptor());
        return Boolean.TRUE;
    }
}